package com.example.demo.controller;
import com.example.demo.response.RegisterResponse;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    @Resource
    UserMapper userMapper;


    @GetMapping("/getList")
    public List<User> getUser(){
        return userMapper.findAll();
    }

    @PostMapping("/rejected")
    public RegisterResponse register(@RequestBody User user){
        User existingUser = userMapper.findByUsername(user.getUsername());
        if (existingUser != null) {
            // 用户名已存在，返回错误信息
            return new RegisterResponse(false,"用户名已存在");
        }
        userMapper.add(user);
        return new RegisterResponse(true,"注册成功");


    }
    @PostMapping("/login")
    public RegisterResponse login(@RequestBody User user) {
        // 从数据库中查找用户
        User existingUser = userMapper.findByUsername(user.getUsername());

        if (existingUser != null && existingUser.getPassword().equals(user.getPassword())) {
            // 登录成功
            return new RegisterResponse(true,"登录成功");

        } else {
            // 登录失败
            return new RegisterResponse(false,"用户名或密码错误");

        }
    }
}
